package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.MessageBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AlertMessageActivity extends RecycleViewActivity {


    private CommonAdapter adapter;

    private List<MessageBean.ArrayBean> itemDatas = new ArrayList<MessageBean.ArrayBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "消息");
        initView();
        refreshLayout.autoRefresh();
    }

    @Override
    public void loadData() {
        APIServiceManager.getInstance().create(MessageApi.class)
                .showMessage(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<MessageBean>() {
                    @Override
                    public void onSuccess(MessageBean value) {

                        if (value != null && value.getArray() != null && value.getArray().size() != 0) {
                            //有数据;
                            if (currentPageIndex == 0) {
                                itemDatas.clear();
                            }
                            itemDatas.addAll(value.getArray());
                            adapter.notifyDataSetChanged();
                        } else {
                            multipleStatusView.showEmpty();
                        }
                        calculatePageIndexAndControlRefreshLayout(value.getArray());

                    }

                    @Override
                    public void onFailure(Throwable e) {
                        multipleStatusView.showNoNetwork();
                    }
                });

    }

    private static final String TAG = "AlertMessageActivity";

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        recyclerview.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new CommonAdapter<MessageBean.ArrayBean>(this, R.layout.activity_alert_message_item, itemDatas) {
            @Override
            protected void convert(final ViewHolder holder, MessageBean.ArrayBean o, final int position) {
             /*  Object o1 = mDatas.get(position);
                String msg = ((Item) o1).getMsg();
                holder.setText(R.id.tv, msg);*/
                View redDot = holder.getView(R.id.redDot);
                if ("1".equals(o.getMes_state())) {
                    redDot.setVisibility(View.VISIBLE);
                } else {
                    redDot.setVisibility(View.INVISIBLE);
                }

                holder.setText(R.id.alertMsgTopTv, o.getMes_title());

                String time = getTimeSafely(o);
                holder.setText(R.id.time, time);

                holder.setText(R.id.alertMsgTopTv, itemDatas.get(position).getMes_title());
                holder.getView(R.id.btnIcCard).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(AlertMessageActivity.this, "onClick: " + position, Toast.LENGTH_SHORT).show();
                        itemDatas.remove(position);
                        adapter.notifyItemRemoved(position);
                        adapter.notifyItemRangeChanged(position, itemDatas.size() - position);
                        SwipeMenuLayout swipeMenuLayout = (SwipeMenuLayout) holder.getConvertView();
                        swipeMenuLayout.quickClose();
                    }
                });
                holder.getView(R.id.activity_alert_message_item).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
            }
        };
        recyclerview.setAdapter(adapter);
    }

    private String getTimeSafely(MessageBean.ArrayBean o) {
        try {

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd");
            long date = Long.parseLong(o.getMes_time());
            String format = simpleDateFormat.format(date);
            return format;
        } catch (Exception e) {
            e.printStackTrace();
            return o.getMes_time();
        }
    }


}
